package com.jilian.mydemo.modul.order.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.support.annotation.Nullable;

import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.OrderDetailDto;
import com.jilian.mydemo.common.dto.OrderDto;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ToastUitl;

public abstract  class BaseOrderDetailActivity  extends BaseActivity {
    protected OrderViewModel orderViewModel;
    protected OrderDto orderDto;
    @Override
    protected void createViewModel() {
        orderViewModel = ViewModelProviders.of(this).get(OrderViewModel.class); 
    }

   
    @Override
    public void initData() {
        orderDto = (OrderDto) getIntent().getSerializableExtra("order");
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(EmptyUtils.isNotEmpty(orderDto)){
            getOrderDetail();
        }
    }

    /**
     * 获取订单详情
     */
    private void getOrderDetail() {
        showLoadingDialog();
        orderViewModel.queryDetail(orderDto.getId());
        orderViewModel.getDetailliveData().observe(this, new Observer<BaseDto<OrderDetailDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<OrderDetailDto> dto) {
                hideLoadingDialog();

                 if(dto.isSuccess()&&EmptyUtils.isNotEmpty(dto)){
                    initDetailView(dto.getData());
                }
                else{
                    ToastUitl.showImageToastTips(dto.getMsg());
                }

            }
        });
    }

    protected abstract void initDetailView(OrderDetailDto data);


}
